package guoanmaker.operator.business.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import guoanmaker.operator.business.model.InRoundPhone;
import guoanmaker.operator.business.model.OperatorCompanyServiceDownNew;
import guoanmaker.operator.business.model.OperatorCompanyServiceNew;
import guoanmaker.operator.business.model.OperatorCompanyServiceNewRound;
import guoanmaker.operator.business.model.ProvideCommodity;
import guoanmaker.operator.business.model.ProvideCommodityServiceRange;
import guoanmaker.operator.business.model.ReturnResult;
import guoanmaker.operator.business.modelnobind.CommodityMove;
import guoanmaker.operator.business.modelnobind.OperatorCommodityRo;
import guoanmaker.operator.business.modelnobind.ProvideCommodityRo;
import guoanmaker.operator.business.repository.OperatorCompanyServiceDownNewRepository;
import guoanmaker.operator.business.repository.OperatorCompanyServiceNewRepository;
/**
 * 
    * @ClassName: OperatorCompanyServiceNewService  
    * @Description: 运营后台企业服务配置  
    * @author lsy  
    * @date 2017年12月17日 下午7:26:02    
    *
 */
@Service
public class OperatorCompanyServiceNewService implements OperatorCompanyServiceNewServiceInterface {
	private static final Object OperatorCommodityRo = null;
	@Autowired
	private OperatorCompanyServiceNewRepository operatorCompanyServiceNewRepository;
	@Autowired
	private OperatorCompanyServiceDownNewRepository operatorCompanyServiceDownNewRepository;
	@PersistenceContext
	private EntityManager manager;
	
	/**
	 * 
	    * @Title: saveOperatorCompanyServiceNew  
	    * @Description: 保存运营后台企业服务 
	    * @param @param inRoundPhone
	    * @throws
	 */
	@Override
	@Transactional
	public void saveOperatorCompanyServiceNew(InRoundPhone inRoundPhone) {
		
		OperatorCompanyServiceNewRound[] companyServiceNewRounds= inRoundPhone.getOperatorCompanyServiceNewRounds();
		
		for (OperatorCompanyServiceNewRound operatorCompanyServiceNewRound : companyServiceNewRounds) {
			OperatorCompanyServiceNew operatorCompanyServiceNew= operatorCompanyServiceNewRound.getOperatorCompanyServiceNew();
			operatorCompanyServiceNew.setCitycode(inRoundPhone.getCityId());
			operatorCompanyServiceNew = operatorCompanyServiceNewRepository.save(operatorCompanyServiceNew);
			List<OperatorCompanyServiceDownNew> companyServiceDownNews = operatorCompanyServiceNewRound.getOperatorCompanyServiceDownNews();
			for (OperatorCompanyServiceDownNew operatorCompanyServiceDownNew : companyServiceDownNews) {
				operatorCompanyServiceDownNew.setCompanyServiceId(operatorCompanyServiceNew.getId());
				operatorCompanyServiceDownNewRepository.save(operatorCompanyServiceDownNew);
				
			}
		}
		
	}
	
	/**
	 * 
	    * @Title: findProvideCommodity  
	    * @Description: 输入商品编号查询商品信息  
	    * @param @param commodityCode
	    * @param @param serviceTypeId
	    * @return OperatorCommodityRo      
	 */

/*	@Override
	public OperatorCommodityRo findProvideCommodity(Integer commodityCode) {
		//根据商品编号查询商品信息
		String jpql = "select new guoanmaker.operator.business.modelnobind.OperatorCommodityRo(p.id,p.commodityName,p.commodityPrice,p.commodity_smallimage,p.commodityState,p.createTime,p.releaseTime,b.businessName,s.serverName,p.salesVolume,p.reasoncontent,p.finalPrice,p.commodityCode)"
				+"from ProvideCommodity p left join p.providerBusiness b  join p.serviceType s where s.serverLevel=2 and p.commodityState=2 ";
		if (commodityCode != null) {
			jpql += "and p.commodityCode=:commodityCode ";
		}
		Query query = manager.createQuery(jpql);
		query.setParameter("commodityCode",commodityCode);
		OperatorCommodityRo operatorCommodityRo = null;
		if (query.getResultList() != null && query.getResultList().size() >0) {
			operatorCommodityRo =  (OperatorCommodityRo)query.getSingleResult();
		}
	
		return operatorCommodityRo;
	}*/
	
	/**
	 * 
	    * @Title: findCommodityByServerTypeId  
	    * @Description: 传入商品编号、二级服务 查询商品属于二级服务   
	    * @param @param commodityCode
	    * @param @param serviceTypeId
	    * @return AjaxJson      
	    * @throws
	 */
/*	@Override
	public ReturnResult findCommodityByServerTypeId(Integer commodityCode, String serviceTypeId) {
		ReturnResult returnResult = new ReturnResult();
		//根据所选二级分类id 判断该商品属于该二级分类 
		String jpql= "select p from ProvideCommodity p left join p.serviceType s where p.commodityCode=:commodityCode and s.id=:serviceTypeId  ";
		Query query = manager.createQuery(jpql);
		query.setParameter("commodityCode", commodityCode);
		query.setParameter("serviceTypeId", serviceTypeId);
		ProvideCommodity provideCommodity = null;
		if (query.getResultList() != null && query.getResultList().size() >0) {
			provideCommodity = (ProvideCommodity)query.getSingleResult();
		}
		if (provideCommodity != null) {
			returnResult.setKey("success");
			returnResult.setValue("输入商品编号属于二级服务");
		} else {
			returnResult.setKey("error");
			returnResult.setValue("您输入的商品编号不属于所选二级分类");
		}
		
		return returnResult;
	}
	*/
	/**
	 * 
	    * @Title: findCommodity  
	    * @Description: 传入商品编号、二级服务 查询商品属于二级服务   
	    * @param @param commodityCode serviceTypeId cityCode
	    * @return  Map<String, Object> 
	    * @throws
	 */
	@Override
	public Map<String, Object> findCommodity(Integer commodityCode, String serviceTypeId, String cityCode) {
		Map<String, Object> map = new HashMap<>();
		ReturnResult returnResult = new ReturnResult();
		String jpql = "select com from ProvideCommodity com left join com.serviceType s where com.commodityState=2 and com.commodityCode=:commodityCode and s.id=:serviceTypeId ";
		Query query = manager.createQuery(jpql);
		query.setParameter("commodityCode", commodityCode);
		query.setParameter("serviceTypeId", serviceTypeId);
		ProvideCommodity provideCommodity = null;
		if (query.getResultList() != null && query.getResultList().size() >0) {
			provideCommodity = (ProvideCommodity)query.getSingleResult();
		}
		if (provideCommodity == null) {
			String jpql1 = "select com from ProvideCommodity com left join com.serviceType s where com.commodityState=2 and com.commodityCode=:commodityCode";
			Query query1 = manager.createQuery(jpql1);
			query1.setParameter("commodityCode", commodityCode);
			if (query1.getResultList() != null && query1.getResultList().size() >0) {
				provideCommodity = (ProvideCommodity)query1.getSingleResult();
			}
			if (provideCommodity == null) {
				returnResult.setKey("error");
				returnResult.setValue("您输入的商品编号不存在");
			}else {
				returnResult.setKey("error");
				returnResult.setValue("您输入的商品编号不属于所选二级分类");
			}
			map.put("error", returnResult);
		} else {//String commodityName, String synopsis, String commodity_smallimage
			ProvideCommodityRo provideCommodityRo  = new ProvideCommodityRo(provideCommodity.getCommodityName(),provideCommodity.getSynopsis(),provideCommodity.getCommodity_smallimage());
			if (provideCommodity.getIsTerritoryRestriction().equals("1")) {
				List<ProvideCommodityServiceRange> commodityServiceRanges = provideCommodity.getServiceRange();
				if (commodityServiceRanges != null && commodityServiceRanges.size() >0) {
					for (ProvideCommodityServiceRange provideCommodityServiceRange : commodityServiceRanges) {
						if (cityCode.equals(provideCommodityServiceRange.getCity().getCode())) {
							provideCommodityRo.setCommodityPrice(provideCommodityServiceRange.getFinalPrice());
							break;
							
						}
					}
				}
				
			} else {
				provideCommodityRo.setCommodityPrice(provideCommodity.getFinalPrice());
			}
			map.put("success", provideCommodityRo);
		}
		

		
		return map;
	}
	
	/**
	 * 
	    * @Title: findAllOperatorCompanyServiceNew  
	    * @Description: 根据cityId 查询企业服务 
	    * @param @param inRoundPhone 下的 cityId
	    * @return AjaxJson      
	    * @throws
	 */
	@Override
	public List<OperatorCompanyServiceNewRound> findAllOperatorCompanyServiceNew(String cityId) {
		List<OperatorCompanyServiceNewRound> operatorCompanyServiceNewRounds = new ArrayList<>();
		OperatorCompanyServiceNewRound companyServiceNewRound = null;
		List<OperatorCompanyServiceNew> companyServiceNews = operatorCompanyServiceNewRepository.findOperatorCompanyServiceNewByCityCode(cityId);
		for (OperatorCompanyServiceNew operatorCompanyServiceNew : companyServiceNews) {
			companyServiceNewRound = new OperatorCompanyServiceNewRound();
			companyServiceNewRound.setOperatorCompanyServiceNew(operatorCompanyServiceNew);
			companyServiceNewRound.setOperatorCompanyServiceDownNews(operatorCompanyServiceDownNewRepository.findCompanyServiceDownNewByCompanyServiceId(operatorCompanyServiceNew.getId()));
			operatorCompanyServiceNewRounds.add(companyServiceNewRound);
			
		}
		return operatorCompanyServiceNewRounds;
	}
	
	/**
	 * 
	    * @Title: deleteOperatorCompanyServiceNewRound  
	    * @Description: 删除企业服务准备从新插入  
	    * @param @param inRoundPhone 下的 cityId
	    * @return AjaxJson      
	    * @throws
	 */
	
	@Override
	@Transactional
	public void deleteOperatorCompanyServiceNewRound(InRoundPhone inRoundPhone) {
		String jpql = "delete from t_operator_company_service_new  where citycode='"+inRoundPhone.getCityId()+"'";
		Query query = manager.createNativeQuery(jpql);
		query.executeUpdate();
		
		
	}
	
	

}
